/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	tcp_transfer.h
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	20221017
**	Function List： 
**  History:         
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	防止重复包含
*********************************************************************************************************
*/

#ifndef _TCP_TRANSFER_H_
#define _TCP_TRANSFER_H_
/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/



/*
*********************************************************************************************************
*                                              	头文件
*********************************************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cmsis_os.h"
/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/
#define		TRANSFER_HEAD		0xFA
#define		TRANSFER_TAIL		0xFB

#define		TRANSFER_PERIODIIC_INTERVAL	10
#define		TRANSFER_RESEND_MAX					3

#define		TRANSFER_SEND_LIST_MAX	16
#define		TRANSFER_SEND_SIZE_MAX	(128)

#define		TRANSFER_ACK_TIMEOUT		2000

#define 	TRANSFER_SEND_DATA_LEN(tot_len)		((tot_len)-sizeof(transfer_head_t)-sizeof(transfer_tail_t))
/*
*********************************************************************************************************
*                                              枚举和结构体类型定义
*********************************************************************************************************
*/
typedef enum{
	TRANSFER_ERR_NONE,
	TRANSFER_ERR_FORMAT,
	TRANSFER_ERR_TIMEOUT,
	TRANSFER_ERR_CRC,
	TRANSFER_ERR_FATAL,
	TRANSFER_ERR_BLOCKED,
}transfer_err_t;

typedef enum{
	TRANSFER_DISCONNECT,
	TRANSFER_WAIT_SYN,
	TRANSFER_IDLE,
	TRANSFER_WAIT_ACK,
}transfer_status_t;

typedef enum{
	/*机械相关故障*/
	MECH_ERR_STALL				=	0x41000000,	/*电机堵转*/
}product_err_code_t;

#pragma pack(1)
typedef struct{
	uint16_t id;
	uint32_t pos;
	uint32_t speed;
	uint16_t relative_pos; /*1表示增量位置*/
}jog_cmd_t;
typedef struct TRANSFER_HEAD_T{
	uint8_t head;
	uint32_t win;
	uint8_t cmd;
	uint16_t length;
	uint32_t id;
}transfer_head_t;

typedef struct TRANSFER_TAIL_T{
	uint16_t modbus_crc;
	uint8_t tail;
}transfer_tail_t;
#pragma pack()
typedef enum{
	CMD_HOME	= 0x00,
	CMD_JOG		= 0x01,
	CMD_LED		= 0x0B,
	
	RPLY_HOME	= 0x10,
	RPLY_JOG	= 0x11,
	RPLY_ID		= 0x1B,
	RPLY_BTN	= 0x1C,
	RPLY_ERROR	= 0x30,
}protocol_cmd_t;
#ifdef PRODUCT_NEW
enum{
	CHANEL_1 = 1,
	CHANEL_2 = 2,
	CHANEL_3 = 0,
};
#else
enum{
	CHANEL_1 = 0,
	CHANEL_2 = 1,
	CHANEL_3 = 2,
};
#endif

typedef struct TRANSFER_SEND_NODE{
	struct TRANSFER_SEND_NODE* next;
	uint16_t	size;
	uint8_t		buff[TRANSFER_SEND_SIZE_MAX];
}transfer_send_node_t;


typedef transfer_err_t (*tcp_input_callback)(protocol_cmd_t cmd,void* buff,uint16_t size);

/*
*********************************************************************************************************
*                                              	全局变量声明
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	函数声明
*********************************************************************************************************
*/
transfer_err_t tcp_transfer_init(tcp_input_callback pfn_callback);
transfer_err_t tcp_transfer_push(protocol_cmd_t cmd,void* buff,uint16_t size);
uint16_t tcp_get_send(uint8_t* buff); /*返回待上传数和长度*/
void transfer_input(uint8_t* buff,uint16_t size);
#endif
